/**
 * Created by Lcp on 2018/5/31.
 */
var eg = {};//使用全单一变量 防止污染
eg.fileArray = [];//防止重复上传
eg.fileData = [];//上传数据
eg.tableData = [];//表单数据附件
eg.tableArea = [];//表单数据区县
eg.fillData = ['南明区','云岩区','花溪区','乌当区','白云区','经开区','开阳县','息烽县','修文县','观山湖区','清镇市'];//区县
eg.dwbm = ['520102','520103','520111','520112','520113','520114','520121','520122','520123','520160','520181'];//单位编码

layui.use('laydate', function(){
    var laydate = layui.laydate;
    //执行一个laydate实例
    laydate.render({
        elem: '#startDate',
        type: 'datetime',
        range: false,
        showBottom : true,
        theme: 'molv',//主题
        min: '1900-1-1 00:00:00',
        max: 0,
        format: 'yyyy-MM-dd',
        done: function(value, date, endDate){
        }
    });
});
getAreaData();
$('.bottomLeft textarea').css({height: ($('.bottomLeft').height() - 150) / 3 + 'px'});
$('.bottomRight textarea').css({height: $('.bottomRight').height() - 218 + 'px'});
window.onresize = function(){
    $('.bottomLeft textarea').css({height: ($('.bottomLeft').height() - 150) / 3 + 'px'});
    $('.bottomRight textarea').css({height: $('.bottomRight').height() - 218 + 'px'});
}

//获取区域数据
function getAreaData(){
    $.ajax({
        type: 'get',
        cache: false,
        url: baseUrls + 'homepage/tjxx',
        beforeSend: function(){
            Loading();
        },
        success: function(response){
            closeLoading();
            if(response.responseCode == '1000'){
                eg.fillData = [];
                eg.dwbm = [];
                for(var i = 0; i < response.content.length; i++){
                    //  eg.fillData.push(response.content[i].qxmc);
                    eg.dwbm.push(response.content[i].qxbh);
                    var edit = '',
                        data = response.content[i];
                    if($('#roleEnName').val() == 'sjgly' || $('#orgCode').val() == data.qxbh){
                        edit = '<a class="layui-btn layui-btn-danger layui-btn-md" lay-event="del" onclick="edit(this,'+ i +')")>编辑</a>';
                    }
                    eg.tableArea.push(
                        {
                            col0: data.qxmc,//区县名称
                            edit: edit,
                            col2: data.aqg == null ? 0 : Number(data.aqg),//安全感
                            col3: data.zdajfas == null ? 0 : Number(data.zdajfas),//重大案件
                            col4: data.nwrqcafs == null ? 0 : Number(data.nwrqcafs),//年万人倾财产案发数
                            col5: data.xxmapal == null ? 0 : Number(data.xxmapal),//现行命案破案书
                            col6: data.bdlxsapal == null ? 0 : Number(data.bdlxsapal),//八类重特大刑事案件破案率
                            col7: data.lqxsxzpal == null ? 0 : Number(data.lqxsxzpal),//\'两抢\'刑事、行政案件破案率
                            col8: data.dqajpal == null ? 0 : Number(data.dqajpal),//盗窃案件破案率
                            col9: data.jcxzpapal == null ? 0 : Number(data.jcxzpapal),//接触性诈骗案件破案率
                            col10: data.fjcxzpapal == null ? 0 : Number(data.fjcxzpapal),//非接触性诈骗案件破案率
                            col11: data.szxxxpal == null ? 0 : Number(data.szxxxpal),//涉众型、新型违法犯罪案件受案率、答复率
                            col12: data.shcexss == null ? 0 : Number(data.shcexss),//扫黑除恶工作(线索)
                            col13: data.shcexjs == null ? 0 : Number(data.shcexjs),//扫黑除恶工作(刑拘)
                            col14: data.shcepas == null ? 0 : Number(data.shcepas),//扫黑除恶工作(破案)
                            col15: data.sqfzxs == null ? 0 : Number(data.sqfzxs),//涉枪违法犯罪(线索)
                            col16: data.sqfzzg == null ? 0 : Number(data.sqfzzg),//涉枪违法犯罪(战果)
                            col17: data.xadfdqpal == null ? 0 : Number(data.xadfdqpal),//小案打防破案率
                            col18: data.xadfjcxzppal == null ? 0 : Number(data.xadfjcxzppal),//小案打防接触性诈骗破案率
                            col19: data.xadfdzpal == null ? 0 : Number(data.xadfdzpal),//小案打防电诈破案率
                            col20: data.qsl == null ? 0 : Number(data.qsl),//盗抢骗工作起诉增长率
                            col21: data.zxl == null ? 0 : Number(data.zxl),//重刑率
                            col22: data.gpqsl == null ? 0 : Number(data.gpqsl),//拐骗工作起诉率
                            col23: data.gpcxal == null ? 0 : Number(data.gpcxal),//撤销案情况
                            col24: data.parls == null ? 0 : Number(data.parls),//破案人脸
                            col25: data.pazws == null ? 0 : Number(data.pazws),//指纹
                            col26: data.padnas == null ? 0 : Number(data.padnas)//DNA
                        }
                    );
                }
                areaTable();
            }else{
                Msg();
            }
        },
        error: function(){
            closeLoading();
            Msg();
        }
    })
}

//图片上传
function getImgURL(node){
    if(node.files && node.files[0] ){
        file = node.files[0];
    }else if(node.files && node.files.item(0)) {
        file = node.files.item(0);
    }
    if(file.type.indexOf('image') >= 0){
        if (eg.fileArray.indexOf(file.name) >= 0){
            Msg('您已选择，如需重复选择请修改名称。');
        } else {
            eg.tableData.push({
                name: file.name.substring(0,file.name.lastIndexOf('.')),
                size: file.size,
                edit: '<a class="layui-btn layui-btn-danger layui-btn-md" lay-event="del" onclick="del('+ eg.fileArray.length +')")>删除</a>'
            });
            eg.fileArray.push(file.name);
            eg.fileData.push(file);
            buildTable();
        }
    }else{
        Msg('请上传图片');
    }
    $(node).val('');
}
//建立表格附件上传
function buildTable(){
    layui.use('table', function(){
        var table = layui.table;
        //展示已知数据
        table.render({
            elem: '#test',
            cols: [[ //标题栏
                {field: 'name', title: '名称', width: '34%', sort: true},
                {field: 'size', title: '大小（kb）', width: '33%', sort: true},
                {field: 'edit', title: '编辑', width: '33%'}
            ]],
            data: eg.tableData,
            even: true
        });
    });
    $('.tableGroup  .layui-table-body').css({height: $('.tableGroup').height() - 54 + 'px'});
}
//建立表格区县信息
//建立表格附件上传
function areaTable(){
    layui.use('table', function(){
        var table = layui.table;
        //展示已知数据
        table.render({
            elem: '#area',
            cols: [[ //标题栏
                {field: 'col0', title: '类别', width: 100},
                {field: 'edit', title: '编辑', width: 100},
                {field: 'col2', title: '群重安全感满意度(%)', width: 175, sort: true,},
                {field: 'col3', title: '重大性案件、敏感性案件', width: 200, sort: true},
                {field: 'col4', title: '年万人侵财类刑事发案数(%)', width: 220, sort: true},
                {field: 'col5', title: '现行命案破案率(%)', width: 220, sort: true},
                {field: 'col6', title: '八类重特大刑事案件破案率(%)', width: 230, sort: true},
                {field: 'col7', title: '\'两抢\'刑事、行政案件破案率(%)', width: 240, sort: true},
                {field: 'col8', title: '盗窃案件破案率(%)', width: 200, sort: true},
                {field: 'col9', title: '接触性诈骗案件破案率(%)', width: 200, sort: true},
                {field: 'col10', title: '非接触性诈骗案件破案率(%)', width: 220, sort: true},
                {field: 'col11', title: '涉众型、新型违法犯罪案件受案率、答复率(%)', width: 330, sort: true},
                {field: 'col12', title: '扫黑除恶工作(线索)', width: 200, sort: true},
                {field: 'col13', title: '扫黑除恶工作(刑拘)', width: 200, sort: true},
                {field: 'col14', title: '扫黑除恶工作(破案)', width: 200, sort: true},
                {field: 'col15', title: '涉枪违法犯罪(线索)', width: 200, sort: true},
                {field: 'col16', title: '涉枪违法犯罪(战果)', width: 200, sort: true},
                {field: 'col17', title: '小案打防破案率(%)', width: 200, sort: true},
                {field: 'col18', title: '小案打防接触性诈骗破案率(%)', width: 200, sort: true},
                {field: 'col19', title: '小案打防电诈破案率(%)', width: 200, sort: true},
                {field: 'col20', title: '盗抢骗工作起诉增长率(%)', width: 200, sort: true},
                {field: 'col21', title: '重刑率(%)', width: 110, sort: true},
                {field: 'col22', title: '拐骗工作起诉率(%)', width: 200, sort: true},
                {field: 'col23', title: '撤销案情况(%)', width: 200, sort: true},
                {field: 'col24', title: '人像', width: 74, sort: true},
                {field: 'col25', title: '指纹', width: 74, sort: true},
                {field: 'col26', title: 'DNA', width: 74, sort: true}
            ]],
            data: eg.tableArea,
            even: true,
            limit: 11
        });
    });
    $('.topLeft  .layui-table-body').css({height: $('.topLeft').height() - 84 + 'px'});
}
//数字验证
function keyPress(ob) {
    if (!ob.value.match(/^[\+\-]?\d*?\.?\d*?$/)) ob.value = ob.t_value; else ob.t_value = ob.value; if (ob.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/)) ob.o_value = ob.value;
    if(ob.value == 'undefined'){
        ob.value = 0;
    }
}
function onBlur(ob) {
    if(!ob.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))ob.value=ob.o_value;else{if(ob.value.match(/^\.\d+$/))ob.value=0+ob.value;if(ob.value.match(/^\.$/))ob.value=0;ob.o_value=ob.value};
    if(ob.value == 'undefined'){
        ob.value = 0;
    }
}
//编辑
function edit(item,Index){
    if($(item).html() == '编辑'){
        $(item).html('保存');
        var length = $(item).parents('tr').find('td').length,
           value = '';
        for(var i = 2; i < length; i++){
            value = eg.tableArea[Index]['col' + i] == 0 ? '' : eg.tableArea[Index]['col' + i];
            $(item).parents('tr').find('td').eq(i).html('<input type="text" value="'+ value +'" class="editInput layui-table-cell laytable-cell-1-col'+ i +'" onkeyup="keyPress(this)" onblur="onBlur(this)"/>');
        }
    }else{
        var length = $(item).parents('tr').find('td').length;
        for(var i = 2; i < length; i++){
            var num = $.trim($(item).parents('tr').find('td').eq(i).find('input').val());
            if(i == 3 || i == 12 || i == 13 || i == 14 || i == 15 || i == 16 || i == 24 || i == 25 || i == 26 ){
                eg.tableArea[Index]['col' + i] = num.length > 0 ? Number(num) : 0;
            }else{
                eg.tableArea[Index]['col' + i] = num.length > 0 ? Number(num).toFixed(2) : 0;
            }
            $(item).parents('tr').find('td').eq(i).html('<div class="layui-table-cell laytable-cell-1-col'+ i +'">'+ eg.tableArea[Index]['col' + i] +'</div>');
        }
        $.ajax({
            type: 'post',
            url: baseUrls + 'homepage/tjxx',
            cache: false,
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify({
                qxbh: eg.dwbm[Index],
                cjr: $('#userCode').val(),
                aqg: eg.tableArea[Index].col2,
                zdajfas: eg.tableArea[Index].col3,
                nwrqcafs: eg.tableArea[Index].col4,
                xxmapal: eg.tableArea[Index].col5,
                bdlxsapal: eg.tableArea[Index].col6,
                lqxsxzpal: eg.tableArea[Index].col7,
                dqajpal: eg.tableArea[Index].col8,
                jcxzpapal: eg.tableArea[Index].col9,
                fjcxzpapal: eg.tableArea[Index].col10,
                szxxxpal: eg.tableArea[Index].col11,
                shcexss: eg.tableArea[Index].col12,
                shcexjs: eg.tableArea[Index].col13,
                shcepas: eg.tableArea[Index].col14,
                sqfzxs: eg.tableArea[Index].col15,
                sqfzzg: eg.tableArea[Index].col16,
                xadfdqpal: eg.tableArea[Index].col17,
                xadfjcxzppal: eg.tableArea[Index].col18,
                xadfdzpal: eg.tableArea[Index].col19,
                qsl: eg.tableArea[Index].col20,
                zxl: eg.tableArea[Index].col21,
                gpqsl: eg.tableArea[Index].col22,
                gpcxal: eg.tableArea[Index].col23,
                parls: eg.tableArea[Index].col24,
                pazws: eg.tableArea[Index].col25,
                padnas: eg.tableArea[Index].col26
            }),
            beforeSend: function(){
                Loading();
            },
            success: function(response){
                closeLoading();
                if(response.responseCode == '1000'){
                    Msg('修改成功',1);
                    $(item).html('编辑');
                }else{
                    Msg();
                }
            },
            error: function(){
                closeLoading();
                Msg();
            }
        })
    }
}
//图片删除
function del(Index){
    layer.msg('确定删除？', {
        time: 0, //不自动关闭
        btn: ['确定', '取消'],
        yes: function(index){
            layer.close(index);
            eg.fileArray.splice(Index,1);
            eg.fileData.splice(Index,1);
            eg.tableData.splice(Index,1);
            buildTable();
        }
    });
}
//图片上传
function uploadMsg(item){
    if(eg.tableData.length > 0){
        var formData = new FormData();
        eg.fileData.forEach(function(item,index){
            formData.append('file',item);
        })
        formData.append('scr',$('#userCode').val());
        $.ajax({
            type: 'post',
            url: baseUrls + 'homepage/fj',
            cache: false,
            contentType: false,
            processData: false,
            data: formData,
            beforeSend: function(){
                Loading();
            },
            success: function(response){
                closeLoading();
                if(response.responseCode == '1000'){
                    Msg('附件上传成功',1);
                    eg.fileArray = [];
                    eg.fileData = [];
                    eg.tableData = [];
                    buildTable();
                }
            },
            error: function(){
                closeLoading();
                Msg();
            }
        })
    }else{
        Msg('请选择附件');
    }
}
function saveFile(item,num){
    if(num == 1){
        if($.trim($('.casename').val()).length > 0){
            $.ajax({
                type: 'post',
                url: baseUrls + 'homepage/zaya',
                cache: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify({
                    ajmc: $.trim($('.casename').val()),
                    cjr: $('#userCode').val(),
                    ajqk: $.trim($('.casetextOne').val()),
                    zbqk: $.trim($('.casetextTwo').val()),
                    gzjh: $.trim($('.casetextTree').val())
                }),
                beforeSend: function(){
                    Loading();
                },
                success: function(response){
                    closeLoading();
                    if(response.responseCode == '1000'){
                        Msg('专案要案上传成功',1);
                        $('.casename').val('');
                        $('.casetextOne').val('');
                        $('.casetextTwo').val('');
                        $('.casetextTree').val('');
                    }
                },
                error: function(){
                    closeLoading();
                    Msg();
                }
            })
        }else{
            Msg('请填写案件名称',5);
        }
    }else{
        if($.trim($('.newName').val()).length > 0){
            $.ajax({
                type: 'post',
                url: baseUrls + 'homepage/zxjq',
                cache: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify({
                    jqmc: $.trim($('.newName').val()),
                    cjr: $('#userCode').val(),
                    jqlx: $.trim($('.newType').val()),
                    afsj: $.trim($('.newDate').val()),
                    jqms: $.trim($('.newText').val())
                }),
                beforeSend: function(){
                    Loading();
                },
                success: function(response){
                    closeLoading();
                    if(response.responseCode == '1000'){
                        Msg('最新警情上传成功',1);
                        $('.newName').val('');
                        $('.newType').val(0);
                        $('.newDate').val('');
                        $('.newText').val('');

                    }
                },
                error: function(){
                    closeLoading();
                    Msg();
                }
            })
        }else{
            Msg('请填写案件名称',5);
        }
    }
}